<?php
//给定课程ID和sectionID，获取某个章节所有资源的存储位置

/*---------------------一、PHP文件引用----------------------------------*/
require_once 'response2.php';
require_once 'connect_config.php';
require('../config.php');

/*---------------------二、变量参数定义-------------------------------*/
$status = false;
$msg = '';
$data = array();

$courseid=$_GET['courseid'];
$sectionid=$_GET['sectionid'];
$token=$_GET['token'];

$serverpath=$_SERVER['SERVER_ADDR'];//服务器IP 
/*
测试数据
$courseid=4;
$sectionid=1;
$token='f47ad7d1e02be1156bf70e786c25cec5';
*/
/*---------------------三、数据库连接--------------------------------*/
$conn = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME,DB_PORT) or die('数据库连接失败');
mysqli_query($conn,"SET NAMES utf8");

/*---------------四、用户ID（确保登录用户才能获取资源）------------------*/
	
$query1 = "select * from mdl_token where token='$token'";
$data1= mysqli_query($conn, $query1);
if($row1 = mysqli_fetch_array($data1))
{
	$userid=$row1['user_id'];
}
else
{
	$status = false;
	$msg = 'cannot find userid in mdl_token';
	echo Response::json($status,$msg,$data);
}

if($userid)
{
	$query2 = "select * from mdl_course_sections where course='$courseid' and section='$sectionid'";
	$data2= mysqli_query($conn, $query2);
	if($row2 = mysqli_fetch_array($data2))
	{
		if($row2['sequence']==NULL)//指定的section没有可用资源
		{
			$status = false;
			$msg = 'there is no resource in specified section!';
			echo Response::json($status,$msg,$data);
		}
		else
		{
			$sequence = explode(',',$row2['sequence']); //将字符串分割为数组
			$index=count($sequence);
			$j=0;
			for($i=0;$i<$index;$i++) 
			{ 
				$query3 = "select * from mdl_context where instanceid='$sequence[$i]' and contextlevel='70'";
				$data3= mysqli_query($conn, $query3);
				
				if($row3 = mysqli_fetch_array($data3))//找到instanceID
				{
//					echo "find instanceid";
					$instanceid=$row3['id'];
					$query4 = "select * from mdl_files where contextid='$instanceid' and component like 'mod_%'";//模糊查询
					$data4= mysqli_query($conn, $query4);
					while($row4 = mysqli_fetch_array($data4))
					{
//						echo "find file<br>";
						if($row4['filename']=='' || strlen($row4['filename'])<4)
						{
							continue;	
						}
						else//找到指定资源的具体名称
						{
							$filename=$row4['filename'];
							echo $filename;
							$filename=urlencode($filename);
							$path1="/";
							
							$data[$j]='http:'.$path1.$path1.$serverpath.$path1.'pluginfile.php'.$path1.$instanceid.$path1.'mod_resource'.$path1.'content'.$path1.'1'.$path1.$filename;
//							$data[$j]=str_replace("\/","/",$data[$j]);
								$j++;
								//echo $data[$j];
								//echo "<br>";
							
						}
 					}
	
				}
				else
				{
					$status = false;
					$msg = 'cannot find squenceid in mdl_context!';
					echo Response::json($status,$msg,$data);
				}
			} 
			$status = true;
			$msg = 'success';
			Response::json($status,$msg,$data);
//			$string=str_replace("\/","/",$string);
//			echo $string;
		}
	}
	else//指定的课程没有选定section（可能是由于sectionid大于该课程所有的section数量)
	{
		$status = false;
		$msg = 'cannot find specified section!';
		echo Response::json($status,$msg,$data);
	}
	
	
}


?>
